package com.example.rumen;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class music extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable updateSeekBar;
    private int currentSongIndex = 0;

    // 模拟歌曲列表
    private final Song[] songs = {
            new Song("Song 1", "Artist 1", R.raw.butterflies),
            new Song("Song 2", "Artist 2", R.raw.ea),
            new Song("Song 3", "Artist 3", R.raw.stj)
    };

    // 控件
    private ImageButton btnPlay, btnPrevious, btnNext;
    private SeekBar seekBar;
    private TextView songTitle, artistName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_music);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化控件
        btnPlay = findViewById(R.id.btnPlay);
        btnPrevious = findViewById(R.id.btnPrevious);
        btnNext = findViewById(R.id.btnNext);
        seekBar = findViewById(R.id.seekBar);
        songTitle = findViewById(R.id.songTitle);
        artistName = findViewById(R.id.artistName);

        // 初始化MediaPlayer
        mediaPlayer = new MediaPlayer();

        // 设置初始歌曲
        playSong(currentSongIndex);

        // 播放/暂停按钮点击事件
        btnPlay.setOnClickListener(v -> {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                btnPlay.setImageResource(R.drawable.play); // 使用XML中定义的图片资源
            } else {
                mediaPlayer.start();
                btnPlay.setImageResource(R.drawable.pause); // 替换为暂停图片
                updateSeekBar();
            }
        });

        // 上一首按钮点击事件
        btnPrevious.setOnClickListener(v -> {
            currentSongIndex = (currentSongIndex > 0) ? currentSongIndex - 1 : songs.length - 1;
            playSong(currentSongIndex);
        });

        // 下一首按钮点击事件
        btnNext.setOnClickListener(v -> {
            currentSongIndex = (currentSongIndex < songs.length - 1) ? currentSongIndex + 1 : 0;
            playSong(currentSongIndex);
        });

        // 进度条拖动事件
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mediaPlayer.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        // 歌曲播放完成事件
        mediaPlayer.setOnCompletionListener(mp -> btnNext.performClick());
    }

    private void playSong(int index) {
        // 重置MediaPlayer
        mediaPlayer.reset();

        try {
            // 设置数据源
            Song song = songs[index];
            mediaPlayer = MediaPlayer.create(this, song.getResourceId());

            // 更新UI
            songTitle.setText(song.getTitle());
            artistName.setText(song.getArtist());
            btnPlay.setImageResource(R.drawable.pause); // 设置播放状态的图片

            // 设置进度条最大值
            seekBar.setMax(mediaPlayer.getDuration());

            // 开始播放
            mediaPlayer.start();

            // 更新进度条
            updateSeekBar();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateSeekBar() {
        seekBar.setProgress(mediaPlayer.getCurrentPosition());

        if (mediaPlayer.isPlaying()) {
            updateSeekBar = () -> updateSeekBar();
            handler.postDelayed(updateSeekBar, 1000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
        if (handler != null) {
            handler.removeCallbacks(updateSeekBar);
        }
    }

    // Song数据类
    private static class Song {
        private final String title;
        private final String artist;
        private final int resourceId;

        public Song(String title, String artist, int resourceId) {
            this.title = title;
            this.artist = artist;
            this.resourceId = resourceId;
        }

        public String getTitle() {
            return title;
        }

        public String getArtist() {
            return artist;
        }

        public int getResourceId() {
            return resourceId;
        }
    }
}
